package com.darren.euc.action.app;

import com.darren.euc.entity.Clue;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.service.ClueService;
import com.darren.euc.service.SpaceService;
import com.darren.euc.util.Kv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * APP接口
 * 空间相关
 * Created by JUECHENG at 2018/3/11 16:22.
 */
@Controller
@RequestMapping("/_space")
public class _SpaceAction {

    @Autowired
    private SpaceService spaceService;
    @Autowired
    private ClueService clueService;

    /**
     * 空间列表
     */
    @ResponseBody
    @RequestMapping(value = "/space_list")
    public ResponseJSON spaceList(
            @RequestParam(name = "city_id",required = false, defaultValue = "0")int city_id,
            @RequestParam(name = "province_id",required = false, defaultValue = "0")int province_id,
            @RequestParam(name = "pn",required = false, defaultValue = "1")int pn,
            @RequestParam(name = "ps",required = false, defaultValue = "10")int ps){

        Kv kv = Kv.of();
        if (city_id != 0)
            kv.set("city_id", city_id);
        if (province_id != 0)
            kv.set("province_id", province_id);

        PageBean page = spaceService.findPage(kv, pn, ps);

        return new ResponseJSON().setData(page);
    }

    /**
     * 空间详情
     */
    @RequestMapping(value = "/space_detail")
    public void spaceDetail(@RequestParam int id, Model model){

        Map map = spaceService.spaceDetail(id);

        model.addAttribute("bean", map);
    }

    @ResponseBody
    @RequestMapping(value = "/space_info")
    public ResponseJSON spaceInfo(@RequestParam int id){
        Map map = spaceService.spaceDetail(id);

        return new ResponseJSON().setData(map);
    }

    /**
     * 预约参观
     * @param space_id 空间id
     * @param contact 预约人
     * @param contact_number 联系电话
     * @param visit_time 预约时间
     */
    @ResponseBody
    @RequestMapping(value = "/space_reserve")
    public ResponseJSON spaceReserve(
            @RequestParam int space_id,
            @RequestParam String contact,
            @RequestParam String contact_number,
            @RequestParam(required = false, defaultValue = "app") String formSourceChannel,
            @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date visit_time){
        
        Clue clue = new Clue();
        clue.setSpaceId(space_id);
        clue.setContact(contact);
        clue.setContactNumber(contact_number);
        clue.setCreateTime(new Date());
        clue.setProductTypeId(1);//产品类型，默认1工位
        clue.setVisitTime(visit_time);

        try{
            clue.setSourceChannelId(Integer.valueOf(formSourceChannel));
        }catch(Exception e){
            clue.setSourceChannelId("app".equals(formSourceChannel)? 6: 8);
        }
        clueService.saveReserve(clue);

        return new ResponseJSON().setData(clue);
    }

}
